import time
from operator import index

from selenium.webdriver.support.select import Select

from selenium.webdriver.support.ui import WebDriverWait

class Base():
    # 初始化驱动
    def __init__(self,driver):
        self.driver=driver
    # 封装打开浏览器的方法
    def open_url(self,url):
        self.driver.get(url)

    def error_png(self):
        error_png=time.strftime('%Y%m%d-%H%M%S')
        return error_png


    # 封装元素定位的方法
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            self.driver.save_screenshot('../png/'+self.error_png()+'.png')
            print(e)
    # 输入事件
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)

    def click(self,loc):
        self.find_element(loc).click()

    def new_window(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])

    def fram(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

    def scroll_x(self,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')

    def scroll_Height(self):
        self.driver.execute_script(f'window.sollTo(0,document.body.scrollHeight)')

    def select_value(self,loc,z):
        value=Select(self.find_element(loc))
        return value.select_by_value(z)

    def select_index(self,loc,z):
        index=Select(self.find_element(loc))
        return index.select_by_index(z)

    def select_text(self,loc,z):
        text=Select(self.find_element(loc))
        return text.select_by_visible_text(z)


# from selenium.webdriver.support.ui import WebDriverWait
#
# import time
# # # 基类
# # class Base():
# #     # 初始化驱动
# #     def __init__(self,driver):
# #         self.driver=driver
# #     # 封装打开网址的方法
# #     def open_url(self,url):
# #         self.driver.get(url)
# #     #
# #     def error_png(self):
# #         error_png=time.strftime('%Y%m%d-%H%M%S')
# #         return error_png
# #
# #     # 封装元素定位的方法
# #     def find_element(self,loc):
# #         try:
# #             WebDriverWait(self.driver,20,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
# #             return self.driver.find_element(*loc)
# #         except Exception as e:
# #             self.driver.save_screenshop('../Png/'+self.error_png()+'.png')
# #             print(e)
# #
# #     # 输入事件
# #     def send_keys(self,loc,info):
# #         self.find_element(loc).send_keys(info)
# #
# #     # 点击事件
# #     def click(self,loc):
# #         self.find_element(loc).click()
# #
# #     # 多窗口切换
# #     def new_window(self):
# #         self.driver.switch_to.window(self.driver.window_handles[-1])
# #
# #     # frame框架
# #     def frame(self,loc):
# #         self.driver.switch_to.frame(self.driver.find_element(loc))
# #
# #     # 下滑
# #     def scroll_x(self,x):
# #         self.driver.execute_script(f'window.scrollTo(0,{x})')
# #
# #     # 滚动到底部
# #     def scroll_Height(self):
# #         self.driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')






